﻿/*********************************************************
 * copyright learnren.com 版权所有
 * 开发人员：minyuan
 * 创建时间：2013/5/10 19:55:14
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;
using LEARNREN.COM.TOOL;
using System.Web.Caching;
using System.Web;

namespace LEARNREN.COM.BLL
{
    public static class BasicCache
    {
        /// <summary>
        /// 网站配置缓存键
        /// </summary>
        const string SCCK = "SiteConfig";
        /// <summary>
        /// 所有栏目列表缓存键
        /// </summary>
        const string ACLK = "AllCatalogueList";
        /// <summary>
        /// 所有栏目面包屑导航缓存键
        /// </summary>
        const string NCDICK = "NCDIC";

        /// <summary>
        /// 缓存中的网站配置信息
        /// </summary>
        public static SiteConfig SiteConfig
        {
            get
            {
                SiteConfig result = System.Web.HttpContext.Current.Cache.Get(SCCK) as SiteConfig;
                if (result == null)
                {
                    try
                    {
                        // 初始化站点基本配置缓存
                        using (SiteLogic siteLogic = new SiteLogic())
                        {
                            result = siteLogic.GetList().FirstOrDefault();
                            HttpContext.Current.Cache.Add(SCCK,
                                                          result,
                                                          null,
                                                          Cache.NoAbsoluteExpiration,
                                                          TimeSpan.FromMinutes(30),
                                                          CacheItemPriority.Normal,
                                                          null);
                        }
                    }
                    catch (Exception ex)
                    {
                        Loger.LogErr(ex);
                        result = new SiteConfig { SiteName = "乐人企业网站管理系统", KeyWords = "乐人企业网站", Summary = "乐人企业网站管理系统" };
                    }
                }

                return result;
            }
            set
            {
                HttpContext.Current.Cache.Insert(SCCK,
                                                 value,
                                                 null,
                                                 Cache.NoAbsoluteExpiration,
                                                 TimeSpan.FromMinutes(30));
            }
        }

        /// <summary>
        /// 缓存中的所有栏目信息
        /// </summary>
        public static List<Catalogue> AllCatalogueList
        {
            get
            {
                List<Catalogue> result = System.Web.HttpContext.Current.Cache.Get(ACLK) as List<Catalogue>;
                if (result == null)
                {
                    try
                    {
                        // 初始化站点基本配置缓存
                        using (CatalogueLogic catalogueLogic = new CatalogueLogic())
                        {
                            result = catalogueLogic.GetListASC(p => p.Order);
                            HttpContext.Current.Cache.Add(ACLK,
                                                          result,
                                                          null,
                                                          Cache.NoAbsoluteExpiration,
                                                          TimeSpan.FromMinutes(30),
                                                          CacheItemPriority.Normal,
                                                          null);
                        }
                    }
                    catch (Exception ex)
                    {
                        Loger.LogErr(ex);
                        result = new List<Catalogue>();
                    }
                }

                return result;
            }
            set
            {
                HttpContext.Current.Cache.Insert(ACLK,
                                                 value,
                                                 null,
                                                 Cache.NoAbsoluteExpiration,
                                                 TimeSpan.FromMinutes(30));
            }
        }

        /// <summary>
        /// 缓存中的所有栏目路径信息
        /// </summary>
        public static AllCatalogueNC AllCatalogueNC
        {
            get
            {
                AllCatalogueNC result = System.Web.HttpContext.Current.Cache.Get(NCDICK) as AllCatalogueNC;
                if (result == null)
                {
                    try
                    {
                        // 初始化站点基本配置缓存
                        result = new AllCatalogueNC();
                        HttpContext.Current.Cache.Add(ACLK,
                                                      result,
                                                      null,
                                                      Cache.NoAbsoluteExpiration,
                                                      TimeSpan.FromMinutes(30),
                                                      CacheItemPriority.Normal,
                                                      null);

                    }
                    catch (Exception ex)
                    {
                        Loger.LogErr(ex);
                        result = new AllCatalogueNC();
                    }
                }

                return result;
            }
            set
            {
                HttpContext.Current.Cache.Insert(ACLK,
                                                 value,
                                                 null,
                                                 Cache.NoAbsoluteExpiration,
                                                 TimeSpan.FromMinutes(30));
            }
        }
    }
}
